In [2]:
couleurs = ["rouge", "orange", "jaune", "vert", "bleu", "indigo", "violet"]
tailles = ["page", "homme", "demi patron", "patron", "grand patron"]

[(couleur, taille) for couleur in couleurs for taille in tailles]


Out[2]:
[('rouge', 'page'),
 ('rouge', 'homme'),
 ('rouge', 'demi patron'),
 ('rouge', 'patron'),
 ('rouge', 'grand patron'),
 ('orange', 'page'),
 ('orange', 'homme'),
 ('orange', 'demi patron'),
 ('orange', 'patron'),
 ('orange', 'grand patron'),
 ('jaune', 'page'),
 ('jaune', 'homme'),
 ('jaune', 'demi patron'),
 ('jaune', 'patron'),
 ('jaune', 'grand patron'),
 ('vert', 'page'),
 ('vert', 'homme'),
 ('vert', 'demi patron'),
 ('vert', 'patron'),
 ('vert', 'grand patron'),
 ('bleu', 'page'),
 ('bleu', 'homme'),
 ('bleu', 'demi patron'),
 ('bleu', 'patron'),
 ('bleu', 'grand patron'),
 ('indigo', 'page'),
 ('indigo', 'homme'),
 ('indigo', 'demi patron'),
 ('indigo', 'patron'),
 ('indigo', 'grand patron'),
 ('violet', 'page'),
 ('violet', 'homme'),
 ('violet', 'demi patron'),
 ('violet', 'patron'),
 ('violet', 'grand patron')]

In [6]:
couleurs_et_tailles = ((couleur, taille) for couleur in couleurs for taille in tailles)

couleurs_et_tailles


Out[6]:
<generator object <genexpr> at 0x11185df10>

In [6]:
couleurs_et_tailles = ((couleur, taille) for couleur in couleurs for taille in tailles)

couleurs_et_tailles


Out[6]:
<generator object <genexpr> at 0x11185df10>

As you can see, it doesn't generate the complete list like the listcomp above. Genexps don't produce entire lists in memory. You need to iterate over them, and then you will get the item one by one. No one is keeping the entire list!


In [7]:
for article in couleurs_et_tailles:
    print(article)


('rouge', 'page')
('rouge', 'homme')
('rouge', 'demi patron')
('rouge', 'patron')
('rouge', 'grand patron')
('orange', 'page')
('orange', 'homme')
('orange', 'demi patron')
('orange', 'patron')
('orange', 'grand patron')
('jaune', 'page')
('jaune', 'homme')
('jaune', 'demi patron')
('jaune', 'patron')
('jaune', 'grand patron')
('vert', 'page')
('vert', 'homme')
('vert', 'demi patron')
('vert', 'patron')
('vert', 'grand patron')
('bleu', 'page')
('bleu', 'homme')
('bleu', 'demi patron')
('bleu', 'patron')
('bleu', 'grand patron')
('indigo', 'page')
('indigo', 'homme')
('indigo', 'demi patron')
('indigo', 'patron')
('indigo', 'grand patron')
('violet', 'page')
('violet', 'homme')
('violet', 'demi patron')
('violet', 'patron')
('violet', 'grand patron')

Next, it's about tuple (in short, not just an immutable list), and slicing. More examples:


In [10]:
jours = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"]
all_slice = slice(None, None, None)
print(jours[all_slice])

every_other_slice = slice(0, None, 2)
print(jours[every_other_slice])

reverse_slice = slice(None, None, -1)
print(jours[reverse_slice])


['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche']
['lundi', 'mercredi', 'vendredi', 'dimanche']
['dimanche', 'samedi', 'vendredi', 'jeudi', 'mercredi', 'mardi', 'lundi']

Next, the chapter moves on to list.sort() vs. sorted(). I kinda like sorted because it works with everything including generators, and it returns a new list. Whereas list.sort() is more limited because it is an in-place sort which returns None.

Next is about bisect, which is interesting but one can read the documentation easily and experiment with it.

Also obviously important is knowing which data structure meets your needs. A bit like knowing the Java Collections Framework again. What I don't really like about Python here is that the nicer and supposedly basic things are not part of the standard library. For example, SortedContainers is by all accounts an excellent package. This is something we've been taking for granted in Java for example.

One notable thing from sorting that's different (and I agree with the author, is nicer) than in other languages is using the "key" argument instead of the usual "Comparator" function/object that returns the standard -1, 0, 1. Again, more examples:


In [6]:
jours = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"] # already ordered

print(sorted(jours))
print(sorted(jours, reverse=True))
print(sorted(jours, key=len, reverse=True)) # by length of string, descend
print(sorted(jours, reverse=True, key=lambda x: jours.index(x))) # by actual order of week, descending


['dimanche', 'jeudi', 'lundi', 'mardi', 'mercredi', 'samedi', 'vendredi']
['vendredi', 'samedi', 'mercredi', 'mardi', 'lundi', 'jeudi', 'dimanche']
['mercredi', 'vendredi', 'dimanche', 'samedi', 'lundi', 'mardi', 'jeudi']
['dimanche', 'samedi', 'vendredi', 'jeudi', 'mercredi', 'mardi', 'lundi']

In [ ]: